package com.ysyc.driver_ee.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.activity.ContactCaptainActivity;
import com.ysyc.driver_ee.activity.LoginActivity;
import com.ysyc.driver_ee.api.HttpResource;
import com.ysyc.driver_ee.api.NetworkCallback;
import com.ysyc.driver_ee.api.RetrofitClient;
import com.ysyc.driver_ee.base.BaseFragment;
import com.ysyc.driver_ee.model.DriverInfo;
import com.ysyc.driver_ee.tools.DateUtils;
import com.ysyc.driver_ee.tools.ToastUtils;
import com.ysyc.driver_ee.widget.NoActionBarView;
import com.ysyc.driver_ee.widget.PromptDiaLog;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * <p>描述：(这里用一句话描述这个类的作用)</p>
 * 作者： Chan<br>
 * 日期： 2018/10/29 18:33<br>
 * 版本： v1.0<br>
 */
public class MineFragment extends BaseFragment {
    @BindView(R.id.iv_headPortrait_view)
    ImageView ivHeadPortraitView;
    @BindView(R.id.tv_name_view)
    TextView tvNameView;
    @BindView(R.id.ll_name_view)
    LinearLayout llNameView;
    @BindView(R.id.tv_car_number_view)
    TextView tvCarNumberView;
    @BindView(R.id.tv_cycle_income_tv)
    TextView tvCycleIncomeTv;
    @BindView(R.id.tv_cumulative_income_view)
    TextView tvCumulativeIncomeView;
    @BindView(R.id.rl_card_view)
    RelativeLayout rlCardView;
    @BindView(R.id.tv_contact_captain_view)
    TextView tvContactCaptainView;
    @BindView(R.id.tv_sign_out_view)
    TextView tvSignOutView;
    @BindView(R.id.no_mine_tv)
    NoActionBarView noMineTv;
    @BindView(R.id.tv_cycle_income_explanation_tv)
    TextView tvCycleIncomeExplanationTv;
    private NetworkCallback<Long> dateNetworkCallback;
    private PromptDiaLog diaLog;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void onFragmentCreate(View view) {
        initNetworkCallback();
        initView();
        noMineTv.setBg(R.drawable.greed_02_1f_rounded_ractangle);
    }

    private void initView() {
        tvContactCaptainView.setOnClickListener(v -> {
            Intent intent = new Intent(getContext(), ContactCaptainActivity.class);
            startActivity(intent);
        });

        tvSignOutView.setOnClickListener(v -> {
            Hawk.deleteAll();
            Intent intent = new Intent(getContext(), LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            getActivity().finish();
        });
        tvCycleIncomeExplanationTv.setOnClickListener(v -> {
            Observable<Long> dateObservable = RetrofitClient.getInstance().getHttpServices().accountTimeNew();
            dateNetworkCallback.postRequest(dateObservable, null);
        });

    }
    private void initNetworkCallback() {
        dateNetworkCallback = new NetworkCallback<>(new HttpResource<Long>() {
            @Override
            public void onSuccess(Long date) {
                getBaseActivity().dismissDilog();
                if (null == diaLog) {
                    diaLog = new PromptDiaLog(getContext());
                }
                diaLog.setStartTime(DateUtils.orderTime(date));
                diaLog.show();
            }

            @Override
            public void onFailure(String errorDes) {
                getBaseActivity().dismissDilog();
            }

            @Override
            public void onLoading(Dialog dialog) {
                getBaseActivity().showLoadingDialog();
            }
        });


        NetworkCallback<DriverInfo> driverInfoNetworkCallback = new NetworkCallback<>(new HttpResource<DriverInfo>() {
            @Override
            public void onSuccess(DriverInfo driverInfo) {
                if (2 == driverInfo.authStatus) {
                    //  司机
                    if (0 == driverInfo.identity) {
                        ivHeadPortraitView.setImageResource(R.drawable.captain_capacity_icon);
                        //负责人
                    } else if (1 == driverInfo.identity) {
                        ivHeadPortraitView.setImageResource(R.drawable.captain_icon);
                    }
                    if (0 == driverInfo.pid) {
                        tvContactCaptainView.setVisibility(View.GONE);
                    } else {
                        tvContactCaptainView.setVisibility(View.VISIBLE);
                    }
                    tvNameView.setText(driverInfo.driverName);
                    tvCycleIncomeTv.setText(driverInfo.totalIncome);
                    tvCumulativeIncomeView.setText(driverInfo.totalIncome);
                    tvCarNumberView.setText(driverInfo.carFormCn + driverInfo.carNumber);
                }

            }

            @Override
            public void onFailure(String errorDes) {
                ToastUtils.show(errorDes);
            }

            @Override
            public void onLoading(Dialog dialog) {

            }
        });
        Observable<DriverInfo> driverInfoObservable = RetrofitClient.getInstance().getHttpServices().obtainDriverInfo();
        driverInfoNetworkCallback.postRequest(driverInfoObservable, null);
    }


}
